'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Enum MetaResponseType As Integer
    OfflineMessageResponse = &H41
    EndOfOfflineMessageResponse = &H42
    MetaInformationResponse = &H7DA
End Enum

Public Enum MetaResponseSubType As Integer
    MetaProcessingErrorServerReply = &H1
    SetUserHomeInfoServerAck = &H64
    SetUserWorkInfoServerAck = &H6E
    SetUserMoreInfoServerAck = &H78
    SetUserNotesInfoServerAck = &H82
    SetUserEmailSInfoServerAck = &H87
    SetUserInterestsInfoServerAck = &H8C
    SetUserAffilationsInfoServerAck = &H96
    ServerSMSResponseDeliveryReceipt = &H96
    SetUserPermissionsServerAck = &HA0
    SetUserPasswordServerAck = &HAA
    UnregisterAccountServerAck = &HB4
    SetUserHomepageCategoryServerAck = &HBE
    UserBasicInfoReply = &HC8
    UserWorkInfoReply = &HD2
    UserMoreInfoReply = &HDC
    UserNotesAboutInfoReply = &HE6
    UserExtendedEmailInfoReply = &HEB
    UserInterestsInfoReply = &HF0
    UserPastAffilationsInfoReply = &HFA
    ShortUserInformationReply = &H104
    UserHomepageCategoryInformationReply = &H10E
    SearchUserFoundReply = &H1A4
    SearchLastUserFoundReply = &H1AE
    RegistrationStatsAck = &H302
    RandomSearchServerReply = &H366
    ServerVariableRequestedViaXml = &H8A2
    ServerAckForSetFullinfoCommand = &HC3F
    ServerAckForUserSpamReport = &H2012
End Enum

Public MustInherit Class MetaResponse
    Implements ISerializable

    Public Const SizeFixPart As Integer = 10

    Public Sub New(ByVal responseType As MetaResponseType)
        _ResponseType = responseType
    End Sub

    Public MustOverride Function CalculateDataSize() As Integer Implements ISerializable.CalculateDataSize

    Public Function CalculateTotalSize() As Integer Implements ISerializable.CalculateTotalSize
        Return SizeFixPart + CalculateDataSize()
    End Function

    Private _DataSize As Integer
    Public ReadOnly Property DataSize() As Integer Implements ISerializable.DataSize
        Get
            Return _DataSize
        End Get
    End Property

    Protected Sub SetDataSize(ByVal index As Integer)
        _DataSize = index - SizeFixPart
    End Sub

    Public ReadOnly Property TotalSize() As Integer Implements ISerializable.TotalSize
        Get
            Return DataSize
        End Get
    End Property

    Private _HasData As Boolean
    Public ReadOnly Property HasData() As Boolean Implements ISerializable.HasData
        Get
            Return _HasData
        End Get
    End Property

    Private _ClientUin As Long
    Public Property ClientUin() As Long
        Get
            Return _ClientUin
        End Get
        Set(ByVal value As Long)
            _ClientUin = value
        End Set
    End Property

    Private _ResponseType As MetaResponseType
    Public Property ResponseType() As MetaResponseType
        Get
            Return _ResponseType
        End Get
        Set(ByVal value As MetaResponseType)
            _ResponseType = value
        End Set
    End Property

    Private _ResponseSequenceNumber As Integer
    Public Property ResponseSequenceNumber() As Integer
        Get
            Return _ResponseSequenceNumber
        End Get
        Set(ByVal value As Integer)
            _ResponseSequenceNumber = value
        End Set
    End Property

    Public Overridable Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte)) Implements ISerializable.Deserialize
        Dim index As Integer

        _DataSize = ByteConverter.ToUInt16LE(data.GetRange(index, 2)) - (SizeFixPart - 2)
        index += 2

        _ClientUin = ByteConverter.ToUInt32LE(data.GetRange(index, 4))
        index += 4

        _ResponseType = DirectCast(CInt(ByteConverter.ToUInt16LE(data.GetRange(index, 2))), MetaResponseType)
        index += 2

        _ResponseSequenceNumber = ByteConverter.ToUInt16LE(data.GetRange(index, 2)) - (SizeFixPart - 2)
        index += 2

        _HasData = True
    End Sub

    Public Overridable Function Serialize() As System.Collections.Generic.List(Of Byte) Implements ISerializable.Serialize
        Throw New NotImplementedException
        'Dim data As List(Of Byte) = New List(Of Byte)

        'data.AddRange(ByteConverter.GetBytesLE(CUShort(Me.CalculateDataSize + (SizeFixPart - 2))))
        'data.AddRange(ByteConverter.GetBytesLE(CUInt(_ClientUin)))
        'data.AddRange(ByteConverter.GetBytesLE(CUShort(_ResponseType)))
        'data.AddRange(ByteConverter.GetBytesLE(CUShort(_ResponseSequenceNumber)))

        'Return data
    End Function
End Class